package com.hschinese.pronunciation.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.interfaces.LessonProgressInterface;
import com.hschinese.pronunciation.interfaces.NextLessonInterface;
import com.hschinese.pronunciation.interfaces.PlayCompleteListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import studychinese.view.ComplexStrokesHandWritingView;

/**
 * Created by buba on 2018/2/5.
 */

public class ComplexStrokesView extends LinearLayout implements PlayCompleteListener{
    private  Context context;
    @Bind(R.id.bsv_view)
    ComplexStrokesHandWritingView bsvView;
    @Bind(R.id.rl_view)
    RelativeLayout rlView;
    /*@Bind(R.id.tv_1)
    TextView tv1;
    @Bind(R.id.tv_2)
    TextView tv2;
    @Bind(R.id.tv_3)
    TextView tv3;
    @Bind(R.id.tv_4)
    TextView tv4;*/
    @Bind(R.id.iv_name)
    ImageView ivName;
    @Bind(R.id.rl_contstrction_parient)
    RelativeLayout rl_contstrction_parient;
    @Bind(R.id.iv_good_hand)
    ImageView ivGoodHand;
    @Bind(R.id.iv_good_star)
    ImageView ivGoodStar;
    @Bind(R.id.rl_good)
    RelativeLayout rlGood;
    private int[] imgRes = {R.drawable.bihua_py_hg, R.drawable.bihua_py_hp, R.drawable.bihua_py_hz, R.drawable.bihua_py_hzz, R.drawable.bihua_py_hzzz, R.drawable.bihua_py_hzt, R.drawable.bihua_py_hzg, R.drawable.bihua_py_hzzg, R.drawable.bihua_py_hwg, R.drawable.bihua_py_hpwg, R.drawable.bihua_py_hzzwg, R.drawable.bihua_py_hzzp, R.drawable.bihua_py_sz, R.drawable.bihua_py_szz, R.drawable.bihua_py_st, R.drawable.bihua_py_sg, R.drawable.bihua_py_szg, R.drawable.bihua_py_szwg, R.drawable.bihua_py_pz, R.drawable.bihua_py_wg};
    private LessonProgressInterface mListener = null;
    private String name;
    private NextLessonInterface mNextListener = null;
    private int currentPosition;
    private Animation.AnimationListener wordViewAnimListener = null;
    private Animation chooseViewAnimation = null;
    // 每个空间都添加一种动画
    long durationMillis = 1000, delayMillis = 0;

    public ComplexStrokesView(Context context) {
        super(context);
        this.context = context;
        initViews(context);
    }

    public ComplexStrokesView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initViews(context);
    }

    private void initViews(Context context) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.complex_strokes_view, null);
        ButterKnife.bind(this, inflate);
        this.addView(inflate);
    }

    public void setData(int position) {
        currentPosition = position;
        rlGood.setVisibility(GONE);
        ivName.setImageResource(imgRes[position]);
        name = "complicated_" + position;
        try {
            bsvView.setText(name);
            bsvView.setDrawListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mListener != null) {
            mListener.setProgress(imgRes.length, position);
        }
        if (currentPosition != 0) {
            addAnimation();
        }
    }

    public void setListener(LessonProgressInterface listener) {
        mListener = listener;
    }

    public void setNextListener(NextLessonInterface listener) {
        mNextListener = listener;
    }

    private void addAnimation() {
        if (rl_contstrction_parient != null) {
            wordViewAnimListener = new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {


                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            };
            chooseViewAnimation = scaleIn(rl_contstrction_parient, durationMillis, delayMillis, wordViewAnimListener);
            rl_contstrction_parient.post(new Runnable() {
                @Override
                public void run() {
                    chooseViewAnimation.start();
                }
            });

        }
    }

    private Animation scaleIn(View view, long durationMillis, long delayMillis, Animation.AnimationListener l) {
        ScaleAnimation animation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(durationMillis);
        animation.setStartOffset(delayMillis);
        view.setVisibility(View.VISIBLE);
        view.startAnimation(animation);
        animation.setAnimationListener(l);
//        animation.cancel();
        return animation;
    }

    private Animation scaleBigIn(View view, long durationMillis, long delayMillis, Animation.AnimationListener l) {
        ScaleAnimation animation = new ScaleAnimation(0, 2, 0, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(durationMillis);
        animation.setStartOffset(delayMillis);
        view.setVisibility(View.VISIBLE);
        view.startAnimation(animation);
        animation.setAnimationListener(l);
        animation.cancel();
        return animation;
    }

    public void beforeWord() {
        if (currentPosition <= 0)
            return;
        currentPosition--;
        if (currentPosition < imgRes.length) {
            setData(currentPosition);
        } /*else {
            // TODO 进入下一课. 会掉到StudyActivity页面来进行View的切换
            // mNextListener.onLessonChanged(lessonType - 1);
            weithLess--;
            currentPosition=0;
            initData();
        }*/
    }

    /* private void updateViewData() {
         if (currentPosition < 0) return;
         if (mListener != null) {
             mListener.setProgress(imgRes.length, currentPosition);
         }
     }*/
    public void nextWord() {
        // TODO 数据切换，涉及到界面上文字和图片的切换

        currentPosition++;
        if (currentPosition < imgRes.length) {
            setData(currentPosition);
        } /*else {
           weithLess++;
           currentPosition=0;
           initData();
           //  Utils.showDialog(mContext, R.string.next_lesson_dialog_msg, lessonType, mNextListener);
       }*/
    }

    /**
     * 用户画完，此时应该有点赞动画出现
     */
    @Override
    public void playComplete() {
        Animation animation1 = AnimationUtils.loadAnimation(context, R.anim.translate_good_star_in);
//        animation1.setStartOffset(500);
        ivGoodStar.setAnimation(animation1);
        rlGood.postDelayed(new Runnable() {
            @Override
            public void run() {
                rlGood.setVisibility(VISIBLE);
            }
        },500);
        scaleIn(ivGoodHand, 300, 500, new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                rlGood.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        rlGood.setVisibility(GONE);
                    }
                },1000);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}
